// Карта на странице маршрута и заказа
var routeMap = {
	markers : [],
	polylines : [],
	opened_windows : [],
	markers_index : 0,
	bounds : {},
	map : {},
	
	 init : function() {
		self = this;
		self.bounds = new google.maps.LatLngBounds();
		mapOptions = {
			panControl: false,
			zoom: 2,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		self.map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

		var markersArray = $('#map-data').data('markers');
		if(markersArray) self.dropMarkers(markersArray); 
		else { // Например на странице поиска маршрута
			self.map.setCenter(new google.maps.LatLng(55, 75));
		}
	},
	
	deleteMarkers : function() {
		self = this;
		if(self.markers.length) {
			for(i in self.markers) {
				self.markers[i].setMap(null);
			}
			for(i in self.polylines) {
				self.polylines[i].setMap(null);
			}
			self.polylines.length = 0;
			self.markers.length = 0;
			self.markers_index = 0;
		}
	},
	
	dropMarkers : function(markersArray) {
		var self = this;
		self.deleteMarkers();
		self.bounds = new google.maps.LatLngBounds();
		for(i in markersArray) {
			if(markersArray[i].no_on_map != 1) {
				var pos = new google.maps.LatLng(markersArray[i].latitude, markersArray[i].longitude);
				var marker =  new google.maps.Marker({ position: pos, animation: google.maps.Animation.DROP, title: markersArray[i].city_name});
				var info = new google.maps.InfoWindow({content:markersArray[i].city_name});
				self.bounds.extend(pos);
				self.markers.push(marker);
			}
			
			
			//var pos = new google.maps.LatLng(markersArray[i].latitude,markersArray[i].longitude);
				//var marker =  new google.maps.Marker({ position: pos,title: markersArray[i].info});
				//bounds.extend(pos);
				//marker.setMap(map)
		}
		//map.setCenter(bounds.getCenter());
	//	map.fitBounds(bounds);
		self.map.setCenter(self.bounds.getCenter());
		self.map.fitBounds(self.bounds);
		for(i in self.markers){
			setTimeout(function(){dropMarker();}, i*1500/self.markers.length);
		}

		function dropMarker(){
			if(self.markers_index < (self.markers.length - 1)){
					drawPath();
			}
			self.markers[self.markers_index++].setMap(self.map);
		}

	    function drawPath(){
	        var start = self.markers[self.markers_index].position;
	        var end = self.markers[self.markers_index+1].position;
	        var points = new Array();
	        points.push(start);
	        points.push(end);
	        self.polylines.push(new google.maps.Polyline({ map: self.map, path: points, strokeColor: "blue", strokeOpacity:0.6 }));
	    }	
	}
};

// Карта на странице гордов
function cityMap() {
	var positionData = $('#map-data').data('position');
	var position = new google.maps.LatLng(positionData.latitude, positionData.longitude);
	var mapOptions = {
		zoom: 10,
		panControl: false,
		center : position,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
	var marker =  new google.maps.Marker({ position: position, animation: google.maps.Animation.DROP});
	marker.setMap(map);
}